import type { WmsWarehouse } from "@/pages/wms/warehouse/entity";
import apiClient from "../../apiClient";
import { UserInfo } from "#/entity";

export interface PageListReq {
	//仓库编码
	code?: string;
	//仓库名称
	name?: string;
	//是否启用
	enableFlag?: string;
	//当前页码
	page: number;
	//每页数量
	pageSize: number;
}

export interface PageListRes {
	//总记录数
	total: number;
	//数据列表
	records: WmsWarehouse[];
}

const pageList = (data: PageListReq): Promise<PageListRes> =>
	apiClient.post({
		url: "/wms/warehouse/pageList",
		data,
	});

const treeList = (data: PageListReq): Promise<WmsWarehouse[]> =>
	apiClient.post({ url: "/wms/warehouse/treeList", data });

const selectList = (): Promise<WmsWarehouse[]> => apiClient.get({ url: "/wms/warehouse/select/list" });

const getInfo = (id: React.Key): Promise<WmsWarehouse> =>
	apiClient.get({
		url: `/wms/warehouse/${id}`,
	});

const add = (data: WmsWarehouse): Promise<WmsWarehouse> =>
	apiClient.post({
		url: "/wms/warehouse",
		data,
	});

const update = (data: WmsWarehouse): Promise<WmsWarehouse> =>
	apiClient.put({
		url: "/wms/warehouse",
		data,
	});

const remove = (ids: React.Key[]): Promise<void> =>
	apiClient.delete({
		url: "/wms/warehouse",
		params: {
			ids: ids.join(","),
		},
	});

const warehouseUsers = (warehouseId: React.Key): Promise<UserInfo[]> =>
	apiClient.get({
		url: `/wms/warehouse/users/${warehouseId}`,
	});

export const wmsWarehouseService = {
	treeList,
	pageList,
	selectList,
	getInfo,
	add,
	update,
	remove,
	warehouseUsers,
};

export default wmsWarehouseService;
